<?php

namespace Ticket\Widget;

class MyTickets implements \Application\Widget {

    /**
     *
     * @var \Zend\Authentication\AuthenticationService
     *
     */
    private $autenticationService;

    /**
     *
     * @var \Ticket\Repository\TicketRepository
     */
    private $ticketRepository;
    
    /**
     * @var \Zend\Mvc\Router\RouteStackInterface; 
     */
    private $router;

    public function render() {
        $user = $this->autenticationService->getIdentity();
        $tickets = $this->ticketRepository->findOpenedByUser($user);

        $widget = '<div><h2>My opened tickets</h2>';

        if (!empty($tickets)) {
            $widget .= '<table><thead><tr><th>Title</th><th>Client</th><th>Time</th><th>&nbsp;</th></thead><tbody>';

            foreach ($tickets as $ticket) {
                $widget.= '<tr><td>' . $ticket->getTitle() . '</td>';
                $widget.= '<td>' . $ticket->getClient()->getDisplayableName() . '</td>';
                $widget.= '<td>' . $ticket->getTime()->format('d-m-Y H:i') . '</td>';
                $widget.= '<td><a href="' . $this->router->assemble(array('id' => $ticket->getId()), array('name' => 'ticket', 'action' => 'view')) .'">View</a></td>';
                    
                $widget.= '</tr>';
            }
            
            $widget .= '</table>';
        } else {
            $widget .= '<p>No opened tickets</p>';
        }

        $widget .= '<a href="' . $this->router->assemble(array(), array('name' => 'reportticket')) . '">Report ticket</a>';
        $widget .= '</div>';        
        
        return $widget;
    }

    public function setAutenticationService($autenticationService) {
        $this->autenticationService = $autenticationService;
    }

    public function setTicketRepository($ticketRepository) {
        $this->ticketRepository = $ticketRepository;
    }
    
    public function setRouter($router) {
        $this->router = $router;
    }

}